<?php
App::uses('AppController', 'Controller');
/**
 * ContaContabils Controller
 *
 * @property ContaContabil $ContaContabil
 */
class ContaContabilsController extends AppController {

	public $paginate = array();
	
/**
 * index method
 *
 * @return void
 */
	public function index() {
		
		$this->paginate['conditions'] = array();
		
		if ($this->request->is('post')) {
			if (!empty($this->request->data['Filter']['descricao'])) {
				$this->Session->write('ContaContabil.descricao', $this->request->data['Filter']['descricao']);
			} else {
				$this->Session->delete('ContaContabil.descricao');
			}
			$this->redirect(array('action' => 'index'));
		}
		
		if ($this->Session->check('ContaContabil.descricao')) {
			$this->request->data['Filter']['descricao'] = $this->Session->read('ContaContabil.descricao');
			if (!empty($this->request->data['Filter']['descricao'])) {
				$this->paginate['conditions'] = array(
					'ContaContabil.descricao ILIKE' => 	'%' . $this->request->data['Filter']['descricao'] . '%',
				);
			}
		}
		
		$this->ContaContabil->recursive = 0;
		$this->set('contaContabils', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->ContaContabil->id = $id;
		if (!$this->ContaContabil->exists()) {
			throw new NotFoundException(__('Invalid conta contabil'));
		}
		$this->request->data = $this->ContaContabil->read(null, $id);
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) { 
			$this->ContaContabil->create();
			if ($this->ContaContabil->save($this->request->data)) {
				$this->Session->setFlash(('A Conta Contábil foi cadastrada com sucesso.'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The conta contabil could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->ContaContabil->id = $id;
		if (!$this->ContaContabil->exists()) {
			throw new NotFoundException(__('Invalid conta contabil'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->ContaContabil->save($this->request->data)) {
				$this->Session->setFlash(('A Conta Contábil foi salva com sucesso.'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The conta contabil could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->ContaContabil->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->ContaContabil->id = $id;
		if (!$this->ContaContabil->exists()) {
			throw new NotFoundException(__('Invalid conta contabil'));
		}
		if ($this->ContaContabil->delete()) {
			$this->Session->setFlash(__('Conta contabil deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Conta contabil was not deleted'));
		$this->redirect(array('action' => 'index'));
	}	
	
	public function autoComplete(){
		$this->autoRender = false;
		$this->layout = null;
		$resultList = $this->ContaContabil->find('list', array(
					'conditions' => array( 'upper(ContaContabil.descricao) LIKE ' => '%'.strtoupper($this->params['url']['term']) . '%'),
					'order' => 'ContaContabil.descricao'
		));
		$busca_realizada = array();
		foreach ($resultList as $id => $value) {
			array_push($busca_realizada, array('label' => ($value), 'value' => $id));
		}
	
		$retorno = json_encode($busca_realizada);
		return $retorno;		
	}
}
